<?php

/**
 * Redis.
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-13 0013
 * @time 14:25:31
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace core;

use core\Log as L;
use core\Config as C;
class Redis
{
    private static $_instance;
    private $config;
    private $log;
    private $redis;
    private $isClose = false;

    public static function getInstance()
    {

        if(! (self::$_instance instanceof self)  )
        {
            self::$_instance = new self();
        }
        return self::$_instance;

    }

    public function __construct()
    {
        $this->config =  C::getInstance();
        $this->log =  L::getInstance('Redis');
        $this->reconnet();
        //$this->ping();
    }

    public function reconnet(){
        if($this->isClose){
            return $this->redis;
        }else{
            //$this->log->info('redis connect');
            $this->redis = new \Redis();
            try{
                $rs = $this->redis->connect($this->config->get('redis.host'),$this->config->get('redis.port'));
            }catch (\Exception $e){
                $this->log->error($e->getMessage());
            }

            if($rs === false){
                $msg = 'Unable to connect to redis'.PHP_EOL;
                $this->log->error($msg);
                exit($msg);
            }
            $pwd = intval($this->config->get('redis.pwd'));
            if($pwd){
                try{
                    $rs = $this->redis->auth($pwd);
                    if(stripos($rs, 'OK') === false){
                        //$this->isClose = false;
                        exit('Redis password error');
                    }
                }catch (\Exception $e){
                    $this->log->error($e->getMessage());
                    exit($e->getMessage());
                }
            }
            $select = intval($this->config->get('redis.select'));
            if($select){
                $this->redis->select($select);
            }
            $this->isClose = true;
            return $this->redis;
        }
    }

    public function ping(){
        //$this->log->info('redis ping');
        try{
            $rs = $this->redis->ping();

            $this->isClose = false;
            if(stripos($rs, 'PONG') === false){
                $this->isClose = true;
            }
        }catch (\Exception $e){
            $this->log->error($e->getMessage());
        }

    }

    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        if(method_exists($this->redis,$name)){
            $this->ping();
            if($this->isClose){
                $this->reconnet();
                self::$_instance = '';
            }
            //$this->log->info('',[$arguments]);
            return call_user_func_array(array($this->redis,$name),$arguments);
        }
        $this->log->error('The redis method does not exist');
        return false;
    }
}